package com.example.demo.genteor;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


import java.util.ResourceBundle;

/**
 * mybait代码生产器
 */
public class MybaitsPlusGenteor {
    private ResourceBundle resourceBundle = ResourceBundle.getBundle("Mybiats-plus");

    private GlobalConfig globalConfig = new GlobalConfig();

    private DataSourceConfig dataSourceConfig = new DataSourceConfig();

    private StrategyConfig strategyConfig = new StrategyConfig();

    private PackageConfig packageConfig = new PackageConfig();

    public void run(){
        AutoGenerator autoGenerator = new AutoGenerator();
        buildGlobalConfig();
        buildDataSourceConfig();
        buildStrategyConfig();
        buildPackageConfig();
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.execute();
    }

    public static void main(String[] args) {
        MybaitsPlusGenteor mybaitsPlusGenteor = new MybaitsPlusGenteor();
        mybaitsPlusGenteor.run();
    }

    public void buildGlobalConfig(){
        globalConfig.setAuthor(resourceBundle.getString("author"));
        globalConfig.setActiveRecord(true);
        globalConfig.setEnableCache(false);
        globalConfig.setBaseResultMap(true);
        globalConfig.setOpen(false);
        globalConfig.setBaseColumnList(true);
        globalConfig.setFileOverride(true);
        globalConfig.setOutputDir(resourceBundle.getString("outPutDir"));
        globalConfig.setMapperName("%sMapper");
        globalConfig.setXmlName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
    }

    public void buildDataSourceConfig(){
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setTypeConvert(new MySqlTypeConvert(){
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                return super.processTypeConvert(fieldType);
            }
        });
        dataSourceConfig.setDriverName(resourceBundle.getString("driverCalss"));
        dataSourceConfig.setUrl(resourceBundle.getString("dburl"));
        dataSourceConfig.setUsername(resourceBundle.getString("dbUserName"));
        dataSourceConfig.setPassword(resourceBundle.getString("dbPassWord"));
    }

    public  void buildStrategyConfig(){
        strategyConfig.setDbColumnUnderline(true);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setTablePrefix(new String[]{resourceBundle.getString("tablePrefix")});
        strategyConfig.setInclude(new String[]{resourceBundle.getString("tableName")});
    }

    public void buildPackageConfig(){
        packageConfig.setParent(resourceBundle.getString("package"));
    }
}
